package cn.quickcode.monitor.model;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import cn.quickcode.monitor.db.DbHelper;

public class Ntm extends ModelSuppert
{
	public final static String TASK_ID = "cn.quickcode.monitor.ntm";
	private Integer id;
	private String name;
	private String ip;
	public Ntm(){}
	public final static List<Ntm> query(String name)
	{
		QueryRunner runner = new QueryRunner();
		String sql=null;
		if(name!=null && name.length()>0)
			sql = "select * from ntm where name like ?";
		else
			sql = "select * from ntm";
		Connection conn = null;
		try
		{
			conn = DbHelper.getConnection();
			List<Ntm> list=null;
			if(name!=null && name.length()>0)
				list = runner.query(conn, sql,new BeanListHandler<Ntm>(Ntm.class),"%"+name+"%");
			else
				list = runner.query(conn, sql, new BeanListHandler<Ntm>(Ntm.class));
			return list;
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			DbHelper.close(conn);
		}
		return null;
	}
	public final static boolean save(Ntm ntm)
	{
		String sql="insert into ntm(name,ip) values(?,?)";
		Connection conn=null;
		QueryRunner runner = new QueryRunner();
		try
		{
			conn = DbHelper.getConnection();
			int i = runner.update(conn, sql,ntm.getName(),ntm.getIp());
			if(i > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		finally
		{
			DbHelper.close(conn);
		}
		return false;
	}
	/**
	 * @return the id
	 */
	public Integer getId()
	{
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(Integer id)
	{
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName()
	{
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	/**
	 * @return the ip
	 */
	public String getIp()
	{
		return ip;
	}
	/**
	 * @param ip the ip to set
	 */
	public void setIp(String ip)
	{
		this.ip = ip;
	}
}
